package me.zhengjie.modules.web.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.zhengjie.config.entity.ResponseBaseEntity;
import me.zhengjie.config.enums.AtmrErrorEnum;
import me.zhengjie.config.exception.WeChatException;
import me.zhengjie.modules.web.domain.VerifyCodeRequest;
import me.zhengjie.modules.web.enums.VerifyCodeSceneEnum;
import me.zhengjie.modules.web.service.IVerifyCodeService;
import me.zhengjie.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 验证码
 */
@RestController
@RequestMapping("web/verifyCode")
@Api(tags = "前端接口-验证码")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    @PostMapping("sendCode")
    @ApiOperation(value = "WEB-发送验证码",notes = "场景值修改手机号=ModifyMobile;签到=SignIn")
    public ResponseBaseEntity sendCode(@RequestBody VerifyCodeRequest requestEntity){
        ResponseBaseEntity response = new ResponseBaseEntity();
        try {
            if (StringUtils.isAnyBlank(requestEntity.getMobile(),requestEntity.getScene())){
                throw new WeChatException("缺少必传参数");
            }
            VerifyCodeSceneEnum enumByScene = VerifyCodeSceneEnum.getEnumByScene(requestEntity.getScene());
            if (null == enumByScene){
                throw new WeChatException("场景值传递不合法");
            }
            boolean result = verifyCodeService.sendVerifyCode(requestEntity,enumByScene);
            response.setBody(result);
        }catch (WeChatException e){
            response.getHeader().setErrCode("-1");
            response.getHeader().setErrMsg(e.getMsg());
        }catch (Exception e){
            response.getHeader().setErrCode(AtmrErrorEnum.EC_ALL_99999.getErrorCode());
            response.getHeader().setErrMsg(AtmrErrorEnum.EC_ALL_99999.getErrorMessage());
        }
        return response;
    }
}
